<?php

namespace app\shell;

use app\model\DeviceBaseModel;
use app\model\OssOrderModel;
use app\model\UrlSelectorModel;
use app\model\VirtualDeviceModel;
use share\console\CLIRunner;
use share\model\IdMakerModel;
use share\model\UserBaseModel;
use share\service\IdMakerService;

class CIdMaker extends CLIRunner
{
    public function test_action()
    {
        $id = IdMakerService::makeMutil("test123", 12);
        return $id;
    }

    /**
     * idMaker 同步
     */
    public function async_action()
    {
        $model = new IdMakerModel;
        $userBase = new UserBaseModel;
        $num = $userBase->max('id');
        if ($num > 0) {
            IdMakerService::make(IdMakerService::NAME_OF_USER_BASE, $num + 1);
            // $model->update(['num' => $num], [
            //     'name' => IdMakerService::NAME_OF_USER_BASE,
            //     'num' => ['<',$num],
            // ]);
        }

        $virtualDevice = new VirtualDeviceModel;
        $num = $virtualDevice->max('id');
        if ($num > 0) {
            IdMakerService::make(IdMakerService::NAME_OF_VIRTUAL_DEVICE, $num + 1);
            // $model->update(['num' => $num], [
            //     'name' => IdMakerService::NAME_OF_VIRTUAL_DEVICE,
            //     'num' => ['<',$num],
            // ]);
        }

        $deviceBase = new DeviceBaseModel;
        $num = $deviceBase->max('id');
        if ($num > 0) {
            IdMakerService::make(IdMakerService::NAME_OF_DEVICE_BASE, $num + 1);
            // $model->update(['num' => $num], [
            //     'name' => IdMakerService::NAME_OF_DEVICE_BASE,
            //     'num' => ['<',$num],
            // ]);
        }

        $ossOrder = new OssOrderModel;
        $num = $ossOrder->max('id');
        if ($num > 0) {
            IdMakerService::make(IdMakerService::NAME_OF_OSS_ORDER, $num + 1);
            // $model->update(['num' => $num], [
            //     'name' => IdMakerService::NAME_OF_OSS_ORDER,
            //     'num' => ['<',$num],
            // ]);
        }

        $urlSelector = new UrlSelectorModel;
        $num = $urlSelector->max('id');
        if ($num > 0) {
            IdMakerService::make(IdMakerService::NAME_OF_URL_SELECTOR, $num + 1);
            // $model->update(['num' => $num], [
            //     'name' => IdMakerService::NAME_OF_URL_SELECTOR,
            //     'num' => ['<',$num],
            // ]);
        }

        $groupNum = $urlSelector->max('group_id');
        if ($groupNum > 0) {
            IdMakerService::make(IdMakerService::NAME_OF_URL_SELECTOR_GROUP, $num + 1);
            // $model->update(['num' => $num], [
            //     'name' => IdMakerService::NAME_OF_URL_SELECTOR_GROUP,
            //     'num' => ['<',$num],
            // ]);
        }
    }
}
